package leetcode;

/***
 * 题目：轴对称二叉树
 * 链接：https://leetcode.cn/problems/symmetric-tree/description/
 */
public class IsSymmetric {

    public static class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {
        }
        TreeNode(int val) {
            this.val = val;
        }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        TreeNode root = getTreeNode();
        System.out.println(IsSymmetric(root));
    }
    public static boolean IsSymmetric(TreeNode root){
        if (root == null) {
            return true;
        } else {
            return recursion(root.left, root.right);
        }
    }
    public static boolean recursion(TreeNode left,TreeNode right) {
        if (left== null && right == null) {
            return true;
        }
        if (left == null || right == null|| left.val != right.val) {
            return false;
        }
        return recursion(left.left, right.right) && recursion(left.right, right.left);
    }
    public static TreeNode getTreeNode(){
        TreeNode root = new TreeNode(1);
        // 第二层节点
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        // 第三层节点
        root.left.right = new TreeNode(3);  // 左子节点的右孩子（数组索引4）
        root.right.right = new TreeNode(3);
        return root;
    }
}
